using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
  public  class tbphanquyenDAL
    {
       private SqlConnection cn = null;
        public tbphanquyenDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbphanquyenDAL(tbphanquyenDAL phanquyen )
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbphanquyen  phanquyen)
        {
            SqlCommand cm = new SqlCommand("phanquyen_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maloainguoidung", phanquyen.Maloainguoidung   );
            cm.ExecuteNonQuery();
        }
        public void Update(tbphanquyen phanquyen)
        {
            SqlCommand cm = new SqlCommand("phanquyen_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maphanquyen", phanquyen.Maphanquyen );
            cm.Parameters.AddWithValue("@maloainguoidung", phanquyen.Maloainguoidung );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("phanquyen_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maphanquyen", iD);
            cm.ExecuteNonQuery();
        }
      public tbphanquyencolection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("phanquyen_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbphanquyencolection phanquyenCollection = new tbphanquyencolection();
            while (dr.Read())
            {
                tbphanquyen phanquyen = new tbphanquyen();
                phanquyen .Maphanquyen = int.Parse(dr.GetValue(0).ToString());
                phanquyen.Maloainguoidung = int.Parse(dr.GetValue(0).ToString());
               phanquyenCollection   .Add(phanquyen  );
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return phanquyenCollection;
        }
    }
}
